$(function () {
    var picture = $('#picturePreview');
    var submitPicture = $('#submitPicture');
    var submitPictureModal = $('#pictureModal');
    picture.cropper({
      //aspectRatio: 240 / 151,
        viewMode: 1,
        autoCropArea: 0.7
    });

    $('#pictureSelector').on('change', function (e) {
        var input = $(this);
        var files = !!input[0].files ? input[0].files : [];
        if (!files.length) {
            return;
        }
        var file = files[0];
        if (!window.FileReader) {
            window.alert(lang('photograph.1'));
            return;
        }
        if (file.size > 5242880) {
            window.alert(lang('photograph.2'));
            return;
        }
        if (!/^image/.test(file.type)) {
            window.alert(lang('photograph.3'));
            return;
        }

        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onloadend = function (e) {
            picture.attr('src', this.result);
            input.val('');
            submitPictureModal.modal('show');
        };
    });
    submitPictureModal.on('shown.bs.modal', function () {
        formInlineRight(submitPicture);
        picture.cropper('replace', picture.attr('src'));
    });

    var defaultSuccessHandler = function (data) {
        submitPicture.removeAttr('disabled');
        return data.data.filename;
    };

    submitPicture.on('click', function (e) {
        e.preventDefault();

        picture.cropper('getCroppedCanvas', {
            width: 700,
            height: 393.75
        }).toBlob(function (blob) {
            if (blob.size > 3145728) {
                formInlineTip(submitPicture, lang('photograph.4'));
                return;
            }

            if (submitPicture.attr('disabled') !== undefined) {
                return;
            }
            submitPicture.attr('disabled', true);
            var config = submitPicture.data('config');
            var imageType = submitPicture.data('type');
            var formData = new FormData();
            formData.append('file', blob, 'blob.jpeg');
            formData.append('config', config);
            formData.append('hotel', window.currentHotel);
            $.ajax('/Home/Dealer/upload', {
                method: "POST",
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    var imageUrl = defaultSuccessHandler(data);
                    pictureCropperSuccess(imageUrl, imageType);

                },
                error: function (error) {
                    submitPicture.removeAttr('disabled');
                    if (error.responseJSON === undefined) {
                        formInlineTip(submitPicture, lang('tip.system'));
                    } else {
                        formInlineTip(submitPicture, error.responseJSON.info + (error.responseJSON.data ? ',' + error.responseJSON.data : ''));
                    }
                }
            });
        }, 'image/jpeg');
    });
});


$(function () {
    window.hotelConstant = {
        'cover': 1,
        'character': 2,
        'gallery': 3,
        'addpic':4,
        'addNewPic':5,
        'changesPic':6,
        'photoID':7
    };
    $('.sever-agree').on('click',function(){
        $('#agreementModal').show();
    })
    $('.close-sever').on('click',function(){
        $('#agreementModal').hide();
    })
    $('#main-form-container').inputValidate({
        submit: {
            query: '#requestSubmit'
        },
        inputs: [{
            query: '#mobile',
            regexp: window.commonRegexp.mobile,
            message: lang('tip.mobile'),
            emptyMessage: lang('tip.mobileNull')
        }, {
            query: '#name',
            regexp: window.commonRegexp.name,
            message: lang('dealer.lineman'),
            emptyMessage: lang('book.linkmanNull')
        }, {
            query: '#dealerID',
            regexp: window.commonRegexp.identity,
            message: lang('dealer.ID'),
            emptyMessage: lang('dealer.IDnull')
        }, {
            query: '#remark',
            regexp: window.commonRegexp.lenght,
            message: lang('dealer.remark')
        }]
    });
    
    $('.pic-add-ID').on('click',function(){
        $('#submitPicture').data('config', 'applydealer').data('type', window.hotelConstant.photoID);
        $('#pictureSelector').trigger('click'); 
    });
    $('#requestSubmit').on('successSubmit.amos.inputValidate', function (e) {
        var $url = $('.pic-null-ID').attr('url');
        if($url == undefined){
            $('.pic-null-ID').css('border-color','#f00');
            $('.pic-null-ID').closest('.form-group').find('label').css('color','#f00');
            $('#apply-tip').show().text(lang('dealer.upload')).css('color','#f00');
            return false;
        }else{
            $('.pic-null-ID').css('border-color','#ccc');
            $('.pic-null-ID').closest('.form-group').find('label').css('color','#000');
            $('#apply-tip').hide();
        }
        var submitBtn = $(this);
        // 该处可放ajax请求
        submitBtn.attr('disabled', true);
        var params = {};
        params.idcardpath = $('.pic-null-ID').attr('url');
        params.mobile = $('#mobile').val();
        params.name = $('#name').val();
        params.idcard = $('#dealerID').val();
        params.remark = $('#remark').val();
        params.areaCode = $('.areaCodeValue').text().trim();
        ajaxRequest('/Home/Dealer/apply', params, 'post', function (data) {
            formInlineTip($('#requestSubmit'), lang('dealer.tip'), 2);
            window.setTimeout(function () {
                window.location.href = 'dealer.html';
            }, 1500);
        },null, submitBtn);
    });
});
var pictureCropperSuccess = function (imageUrl, imageType) {
    var submitPicture = $('#submitPicture');
    var $picCon =  $('#character-panel .vertical-margin').find('.picture-container');
    switch (imageType) {
        case window.hotelConstant.photoID:
            $('.photoID').attr('style','background-image:url('+ imageUrl+")");
            $('.pic-null-ID').attr('url',imageUrl);
            $('#pictureModal').modal('hide');
            break;
        default:
            break;
    }
};